package com.hw.protocol.tool.controller.transform;

import com.hw.protocol.tool.decode.hw.*;
import com.hw.protocol.tool.model.entity.HwProtocol;
import com.hw.protocol.tool.model.entity.MeterData;
import com.hw.protocol.tool.model.entity.hw.*;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;

/**
 * @author : lihy
 * @date : 2024/3/14 15:10
 * description:
 */
@Slf4j
public class HwTransform extends AbsTransform {

    public static String transform(Object data) {
        HwProtocol protocol = (HwProtocol) data;
        List<MeterData> meterDataList = new LinkedList<>();
        if (protocol.getFunctionCode() == 0x10) {
            setRealData(protocol, meterDataList);
        } else if (protocol.getFunctionCode() == 0x11) {
            setHistoryData(protocol, meterDataList);
        } else {
            return "不支持数据转换";
        }
        return getJson(meterDataList);
    }

    private static void setHistoryData(HwProtocol protocol, List<MeterData> meterDataList) {
        switch (protocol.getChunnelSerial()) {
            case "5000":
            case "5100":
                Dtu50His dtu50His = new Dtu50His();
                dtu50His.setDevSerial(protocol.getDevSerial());
                dtu50His.setChunnelSerial(protocol.getChunnelSerial());
                Dtu50HisDecode dtu50HisDecode = new Dtu50HisDecode(dtu50His, protocol.getData());
                dtu50HisDecode.buildObject();
                log.debug("接收流量计[{}, {}]历史数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), dtu50His.toString());
                meterDataList.add(HwTransformUtil.transform(dtu50His));
                break;
            case "6130":
            case "6131":
                Dtu61His dtu61His = new Dtu61His();
                dtu61His.setDevSerial(protocol.getDevSerial());
                dtu61His.setChunnelSerial(protocol.getChunnelSerial());
                Dtu61HisDecode dtu61HisDecode = new Dtu61HisDecode(dtu61His, protocol.getData());
                dtu61HisDecode.buildObject();
                log.debug("接收流量计[{}, {}]历史数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), dtu61His.toString());
                break;
            case "6020":
            case "6019":
                Dtu60His dtu60His = new Dtu60His();
                dtu60His.setDevSerial(protocol.getDevSerial());
                dtu60His.setChunnelSerial(protocol.getChunnelSerial());
                Dtu60HisDecode dtu60HisDecode = new Dtu60HisDecode(dtu60His, protocol.getData());
                dtu60HisDecode.buildObject();
                log.debug("接收压力计[{}, {}]历史数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), dtu60His.toString());
                meterDataList.addAll(HwTransformUtil.transform(dtu60His));
                break;
            case "6010":
                RegulatorBoxHis boxHis = new RegulatorBoxHis();
                boolean flag = new RegulatorBoxHisDecode(boxHis, protocol.getData()).buildObject();
                if (flag) {
                    boxHis.setDevSerial(protocol.getDevSerial());
                    boxHis.setChunnelSerial(protocol.getChunnelSerial());
                    log.debug("接收调压箱[{}, {}]历史数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), boxHis.toString());
                    meterDataList.add(HwTransformUtil.transform(boxHis));
                }
                break;
            case "10000":
                AlertorHis alertorHis = new AlertorHis();
                new AlertorHisDecode(alertorHis, protocol.getData()).buildObject();
                alertorHis.setDevSerial(protocol.getDevSerial());
                alertorHis.setChunnelSerial(protocol.getChunnelSerial());
                log.debug("接收商业报警器[{}, {}]历史数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), alertorHis.toString());
                meterDataList.addAll(HwTransformUtil.transform(alertorHis));
                break;
            default:
                break;
        }
    }

    private static void setRealData(HwProtocol protocol, List<MeterData> meterDataList) {
        switch (protocol.getChunnelSerial()) {
            case "5000":
            case "5100":
                Dtu50 dtu50 = new Dtu50();
                dtu50.setDevSerial(protocol.getDevSerial());
                dtu50.setChunnelSerial(protocol.getChunnelSerial());
                Dtu50Decode dtu50Decode = new Dtu50Decode(dtu50, protocol.getData());
                dtu50Decode.buildObject();
                log.debug("接收流量计[{}, {}]实时数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), dtu50.toString());
                meterDataList.add(HwTransformUtil.transform(dtu50));
                break;
            case "6130":
            case "6131":
                Dtu61 dtu61 = new Dtu61();
                dtu61.setDevSerial(protocol.getDevSerial());
                dtu61.setChunnelSerial(protocol.getChunnelSerial());
                Dtu61Decode dtu61Decode = new Dtu61Decode(dtu61, protocol.getData());
                dtu61Decode.buildObject();
                log.debug("接收流量计[{}, {}]实时数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), dtu61.toString());
                break;
            case "6020":
            case "6019":
                Dtu60 dtu60 = new Dtu60();
                dtu60.setDevSerial(protocol.getDevSerial());
                dtu60.setChunnelSerial(protocol.getChunnelSerial());
                Dtu60Decode dtu60Decode = new Dtu60Decode(dtu60, protocol.getData());
                dtu60Decode.buildObject();
                log.debug("接收压力计[{}, {}]实时数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), dtu60.toString());
                meterDataList.addAll(HwTransformUtil.transform(dtu60));
                break;
            case "6010":
                RegulatorBox box = new RegulatorBox();
                new RegulatorBoxHisDecode(box, protocol.getData()).buildObject();
                box.setDevSerial(protocol.getDevSerial());
                box.setChunnelSerial(protocol.getChunnelSerial());
                log.debug("接收调压箱[{}, {}]实时数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), box.toString());
                meterDataList.add(HwTransformUtil.transform(box));
                break;
            case "10000":
                Alertor alertor = new Alertor();
                new AlertorDecode(alertor, protocol.getData()).buildObject();
                alertor.setDevSerial(protocol.getDevSerial());
                alertor.setChunnelSerial(protocol.getChunnelSerial());
                log.debug("接收商业报警器[{}, {}]实时数据：{}", protocol.getDevSerial(), protocol.getChunnelSerial(), alertor.toString());
                meterDataList.addAll(HwTransformUtil.transform(alertor));
                break;
            default:
                break;
        }
    }
}
